/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.form;

import model.Storage;
import domen.Korisnik;
import javax.swing.JOptionPane;
import model.StorageBroker;

/**
 *
 * @author Vlada
 */
public class JPanelKorisnik extends javax.swing.JPanel {

    StorageBroker sb;
    String ime;
    String prezime;
    String korisnickiIme;
    String email;
    String korisnickaSifra;
    Korisnik odabraniKorisnik;

    /**
     * Creates new form JPanelDodajNovogKorisnika
     */
    public JPanelKorisnik() {
        initComponents();
        srediFormu();
        jbtnIzmeniKorisnika.setVisible(false);
        jbtnObrisiKorisnika.setVisible(false);
    }

    public JPanelKorisnik(Korisnik korisnik) {
        initComponents();
        srediFormu();
        sb = new StorageBroker(new Korisnik());
        jbtnSacuvajKorisnika.setVisible(false);

        odabraniKorisnik = korisnik;

        jpImeKorisnika.vratiKomponentuTexta().setText(odabraniKorisnik.getIme());
        jpPrezime.vratiKomponentuTexta().setText(odabraniKorisnik.getPrezime());
        jpEmail.vratiKomponentuTexta().setText(odabraniKorisnik.getEmail());
        jpKorisnickoIme.vratiKomponentuTexta().setText(odabraniKorisnik.getKorisickoIme());
        jpKorisnickaSifra.vratiKomponentuTexta().setText(odabraniKorisnik.getKorisnickaSifra());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jpAkcije = new javax.swing.JPanel();
        jbtnSacuvajKorisnika = new javax.swing.JButton();
        jbtnIzmeniKorisnika = new javax.swing.JButton();
        jbtnObrisiKorisnika = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jpImeKorisnika = new ui.components.JPanelTextField();
        jpPrezime = new ui.components.JPanelTextField();
        jpEmail = new ui.components.JPanelTextField();
        jpKorisnickoIme = new ui.components.JPanelTextField();
        jpKorisnickaSifra = new ui.components.JPanelPasswordField();

        jpAkcije.setBorder(javax.swing.BorderFactory.createTitledBorder("Akcije"));

        jbtnSacuvajKorisnika.setText("Sacuvaj");
        jbtnSacuvajKorisnika.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnSacuvajKorisnikaActionPerformed(evt);
            }
        });

        jbtnIzmeniKorisnika.setText("Izmeni");
        jbtnIzmeniKorisnika.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnIzmeniKorisnikaActionPerformed(evt);
            }
        });

        jbtnObrisiKorisnika.setText("Obrisi");
        jbtnObrisiKorisnika.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnObrisiKorisnikaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jpAkcijeLayout = new javax.swing.GroupLayout(jpAkcije);
        jpAkcije.setLayout(jpAkcijeLayout);
        jpAkcijeLayout.setHorizontalGroup(
            jpAkcijeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpAkcijeLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpAkcijeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jbtnSacuvajKorisnika, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbtnIzmeniKorisnika, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbtnObrisiKorisnika, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jpAkcijeLayout.setVerticalGroup(
            jpAkcijeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpAkcijeLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jbtnSacuvajKorisnika)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnIzmeniKorisnika)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnObrisiKorisnika)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Informacije o korisniku"));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jpImeKorisnika, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jpPrezime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jpEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jpKorisnickoIme, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jpKorisnickaSifra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jpImeKorisnika, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpPrezime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpKorisnickoIme, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpKorisnickaSifra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 34, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jpAkcije, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jpAkcije, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnSacuvajKorisnikaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnSacuvajKorisnikaActionPerformed

        uzmiPodatkeSaForme();

        if (validacijaKorisnika()) {

            Korisnik korisnik = new Korisnik();
            korisnik.setIme(ime);
            korisnik.setPrezime(prezime);
            korisnik.setEmail(email);
            korisnik.setKorisickoIme(korisnickiIme);
            korisnik.setKorisnickaSifra(korisnickaSifra);

            StorageBroker sb = new StorageBroker(korisnik);
            sb.ubaciEntitetUMapu();
            sb.ispisiMapu();
            JOptionPane.showMessageDialog(this, "Korisnik je uspesno sacuvan");

            resetujPoljaNaFormi();
        }

    }//GEN-LAST:event_jbtnSacuvajKorisnikaActionPerformed

    private void jbtnIzmeniKorisnikaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnIzmeniKorisnikaActionPerformed
        uzmiPodatkeSaForme();

        if (validacijaKorisnika()) {
            odabraniKorisnik.setIme(ime);
            odabraniKorisnik.setPrezime(prezime);
            odabraniKorisnik.setEmail(email);
            odabraniKorisnik.setKorisickoIme(korisnickiIme);
            odabraniKorisnik.setKorisnickaSifra(korisnickaSifra);

            JOptionPane.showMessageDialog(this, "Podaci korisnika su uspesno izmenjeni.");
            sb.ispisiMapu();
            Storage.getInstance().getGlavnaForma().ucitajPanel(new JPanelKorisniciPrikaz());
        }
    }//GEN-LAST:event_jbtnIzmeniKorisnikaActionPerformed

    private void jbtnObrisiKorisnikaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnObrisiKorisnikaActionPerformed
        sb.izbrisiEntitet(odabraniKorisnik.getKorisickoIme());
        JOptionPane.showMessageDialog(this, "Korisnik je uspesno izbrisan.");

        Storage.getInstance().getGlavnaForma().ucitajPanel(new JPanelKorisniciPrikaz());
    }//GEN-LAST:event_jbtnObrisiKorisnikaActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jbtnIzmeniKorisnika;
    private javax.swing.JButton jbtnObrisiKorisnika;
    private javax.swing.JButton jbtnSacuvajKorisnika;
    private javax.swing.JPanel jpAkcije;
    private ui.components.JPanelTextField jpEmail;
    private ui.components.JPanelTextField jpImeKorisnika;
    private ui.components.JPanelPasswordField jpKorisnickaSifra;
    private ui.components.JPanelTextField jpKorisnickoIme;
    private ui.components.JPanelTextField jpPrezime;
    // End of variables declaration//GEN-END:variables

    private void srediFormu() {
        jpImeKorisnika.vratiKomponentuZaPrikazTeksta().setText("Ime:");
        jpImeKorisnika.vratiKomponentuZaPrikazGreske().setText(" ");

        jpPrezime.vratiKomponentuZaPrikazTeksta().setText("Prezime:");
        jpPrezime.vratiKomponentuZaPrikazGreske().setText(" ");

        jpEmail.vratiKomponentuZaPrikazTeksta().setText("Email:");
        jpEmail.vratiKomponentuZaPrikazGreske().setText(" ");

        jpKorisnickoIme.vratiKomponentuZaPrikazTeksta().setText("Korisnicko ime:");
        jpKorisnickoIme.vratiKomponentuZaPrikazGreske().setText(" ");

        jpKorisnickaSifra.vratiKomponentuZaPrikazGreske().setText(" ");

        resetujPoljaNaFormi();
    }

    private boolean validacijaKorisnika() {

        boolean validacija = true;

        try {
            if (ime.isEmpty()) {
                jpImeKorisnika.vratiKomponentuZaPrikazGreske().setText("Polje ime mora biti uneto!");
                //throw new Exception("Validacija nije uspela");
                validacija = false;
            } else {
                jpImeKorisnika.vratiKomponentuZaPrikazGreske().setText(" ");
            }

            if (prezime.isEmpty()) {
                jpPrezime.vratiKomponentuZaPrikazGreske().setText("Polje prezime mora biti "
                        + "uneto!");
                validacija = false;
            } else {
                jpPrezime.vratiKomponentuZaPrikazGreske().setText(" ");
            }

            if (korisnickiIme.isEmpty()) {
                jpKorisnickoIme.vratiKomponentuZaPrikazGreske().setText("Polje korisnicko ime "
                        + "mora biti uneto!");
                validacija = false;
            } else {
                jpKorisnickoIme.vratiKomponentuZaPrikazGreske().setText(" ");
            }

            if (email.isEmpty()) {
                jpEmail.vratiKomponentuZaPrikazGreske().setText("Polje email mora biti uneto!");
                validacija = false;
            } else {
                jpEmail.vratiKomponentuZaPrikazGreske().setText(" ");
            }

            if (korisnickaSifra.isEmpty()) {
                jpKorisnickaSifra.vratiKomponentuZaPrikazGreske().setText("Polje korisnicka sifra "
                        + "mora biti uneto!");
                validacija = false;
            } else {
                jpKorisnickaSifra.vratiKomponentuZaPrikazGreske().setText(" ");
            }

            return validacija;
        } catch (Exception e) {
            System.out.println("Validacija nije uspela!");
            JOptionPane.showMessageDialog(this, e.getMessage());
        }
        return false;
    }

    private void uzmiPodatkeSaForme() {
        ime = jpImeKorisnika.vratiKomponentuTexta().getText().trim();
        prezime = jpPrezime.vratiKomponentuTexta().getText().trim();
        korisnickiIme = jpKorisnickoIme.vratiKomponentuTexta().getText().trim();
        email = jpEmail.vratiKomponentuTexta().getText().trim();

        korisnickaSifra = jpKorisnickaSifra.vratiKomponentuTexta().getPassword().toString();
    }

    private void resetujPoljaNaFormi() {
        jpImeKorisnika.vratiKomponentuTexta().setText("");
        jpPrezime.vratiKomponentuTexta().setText("");
        jpEmail.vratiKomponentuTexta().setText("");
        jpKorisnickoIme.vratiKomponentuTexta().setText("");
        jpKorisnickaSifra.vratiKomponentuTexta().setText("");
    }

}
